pl = require('lib.pl.import_into')()
lume = require('lib.lume')
Class = require('lib.hump.class')
Signal = require('lib.hump.signal')
require('src.gen.macros')

local Moonshine = require('lib.moonshine')
local Effects = Moonshine.chain(Moonshine.effects.crt).chain(Moonshine.effects.chromasep)
local fennel = require('lib.fennel')
table.insert(package.loaders or package.searchers, fennel.searcher)

love.update = function (dt)
   Game:update(dt)
end

love.draw = function ()
   Effects.draw(function ()
	 Game:draw()
   end)
end

love.load = function ()
   love.window.setFullscreen(true)
   local font = love.graphics.newFont("assets/fonts/monogram-extended.ttf", 25)
   font:setFilter("nearest", "nearest")
   love.graphics.setFont(font)
   love.graphics.setDefaultFilter("nearest", "nearest")
   Effects.parameters = {
      crt = {
	 distortionFactor = {1, 1}
      },
      chromasep = {
	 angle = 1.1,
	 radius = .8
      }
   }
   Game = require('src.gen.game')()
   Game:load("deployment", "field")
end

love.resize = function (w, h)
   Game.current_screen:reload_camera ()
   Effects.resize(w, h)
   Game.state:resize(w, h)
end

love.keypressed = function (k) 
   Game.input:press_key(k) 
end

love.keyreleased = function (k)

end

love.mousemoved = function (x, y)
   if Game.state.mousemoved then
      Game.state:mousemoved(x, y)
   end
end

love.mousepressed = function (x, y, b)
   Game.input:press_mouse(b)
   if Game.state.mousepressed then
      Game.state:mousepressed(x, y, b)
   end
end

love.mousereleased = function (x, y, b) 
   Game.input:release_mouse(b)
   if Game.state.mousereleased then Game.state:mousereleased(x, y, b) end
end

love.textinput = function (t) 
   Game.input:ti_press_key (t) 
end

love.wheelmoved = function (x, y) 
   Game.input:wheelmoved(x, y)
   if Game.state.wheelmoved then Game.state:wheelmoved(x, y) end
end

love.quit = function ()
   Game = nil
   local fs = love.window.getFullscreen()
   if fs then love.window.setFullscreen(false) end
   collectgarbage()
end
